package cn.zl.GB28181.event.core;

import cn.hutool.core.util.ObjUtil;
import cn.zl.GB28181.core.DeviceNotFoundEvent;
import gov.nist.javax.sip.message.SIPRequest;
import gov.nist.javax.sip.message.SIPResponse;

import javax.sip.DialogTerminatedEvent;
import javax.sip.ResponseEvent;
import javax.sip.TimeoutEvent;
import javax.sip.TransactionTerminatedEvent;
import javax.sip.header.WarningHeader;

public class EventResult<E> {

    public int statusCode;
    public EventResultType type;
    public String msg;
    public String callId;
    public E event;

    public EventResult() {
    }

    public EventResult(E event) {
        this.event = event;
        if (event instanceof ResponseEvent responseEvent) {
            SIPResponse response = (SIPResponse) responseEvent.getResponse();
            this.type = EventResultType.response;
            if (response != null) {
                WarningHeader warningHeader = (WarningHeader) response.getHeader(WarningHeader.NAME);
                if (warningHeader != null && !ObjUtil.isEmpty(warningHeader.getText())) {
                    this.msg = "";
                    if (warningHeader.getCode() > 0) {
                        this.msg += warningHeader.getCode() + ":";
                    }
                    if (warningHeader.getAgent() != null) {
                        this.msg += warningHeader.getCode() + ":";
                    }
                    if (warningHeader.getText() != null) {
                        this.msg += warningHeader.getText();
                    }
                } else {
                    this.msg = response.getReasonPhrase();
                }
                this.statusCode = response.getStatusCode();
                this.callId = response.getCallIdHeader().getCallId();
            }
        } else if (event instanceof TimeoutEvent timeoutEvent) {
            this.type = EventResultType.timeout;
            this.msg = "消息超时未回复";
            this.statusCode = -1024;
            if (timeoutEvent.isServerTransaction()) {
                this.callId = ((SIPRequest) timeoutEvent.getServerTransaction().getRequest()).getCallIdHeader().getCallId();
            } else {
                this.callId = ((SIPRequest) timeoutEvent.getClientTransaction().getRequest()).getCallIdHeader().getCallId();
            }
        } else if (event instanceof TransactionTerminatedEvent transactionTerminatedEvent) {
            this.type = EventResultType.transactionTerminated;
            this.msg = "事务已结束";
            this.statusCode = -1024;
            if (transactionTerminatedEvent.isServerTransaction()) {
                this.callId = ((SIPRequest) transactionTerminatedEvent.getServerTransaction().getRequest()).getCallIdHeader().getCallId();
            } else {
                this.callId = ((SIPRequest) transactionTerminatedEvent.getClientTransaction().getRequest()).getCallIdHeader().getCallId();
            }
        } else if (event instanceof DialogTerminatedEvent dialogTerminatedEvent) {
            this.type = EventResultType.dialogTerminated;
            this.msg = "会话已结束";
            this.statusCode = -1024;
            this.callId = dialogTerminatedEvent.getDialog().getCallId().getCallId();
        } else if (event instanceof DeviceNotFoundEvent) {
            this.type = EventResultType.deviceNotFoundEvent;
            this.msg = "设备未找到";
            this.statusCode = -1024;
            this.callId = ((DeviceNotFoundEvent) event).getCallId();
        }
    }
}
